package net.hmapp.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import net.hmapp.app.activity.R;
import net.hmapp.app.viewmodel.Channel;

import java.util.List;

public class ChannelAdapter extends BaseAdapter {

    private Context ctx;
    private List<Channel> channels;
    public ChannelAdapter(Context ctx,List<Channel> channelLs) {
        this.ctx= ctx;
        this.channels=channelLs;
    }

    @Override
    public int getCount() {
        return this.channels.size();
    }

    @Override
    public Object getItem(int position) {
        return this.channels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int index, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView==null){
            convertView= LayoutInflater.from(this.ctx).inflate(R.layout.home_channel_item,null);
            viewHolder=new ViewHolder();
            viewHolder.home_channel_txt=convertView.findViewById(R.id.home_channel_txt);
            viewHolder.home_channel_img=convertView.findViewById(R.id.home_channel_img);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.home_channel_txt.setText(this.channels.get(index).getName());
        Glide.with(convertView.getContext())
                .load(this.channels.get(index).getIconUrl())
                .into(viewHolder.home_channel_img);

        return convertView;
    }
    static class  ViewHolder{
        private ImageView home_channel_img;
        private TextView home_channel_txt;

    }
}
